//
//  WiiAddTaskViewController.m
//  Swipes
//
//  Created by Wii on 15/12/8.
//  Copyright © 2015年 Wii. All rights reserved.
//

#import "WiiAddTaskViewController.h"

@interface WiiAddTaskViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIButton *closeAndSendButton;
@property (weak, nonatomic) IBOutlet UITextField *inputTextFiled;
@property (nonatomic , assign) CGSize keyboardSize;
@property (nonatomic , strong) XYSqlite3 *sql;
@property (nonatomic , assign) BOOL isSend;
@end


typedef NS_ENUM (NSInteger,WiiCloseButtonStatus){
    WiiCloseButtonStatusSend = 100,
    WiiCloseButtonStatusCancel
    
};

@implementation WiiAddTaskViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];

//    [self.inputText becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.sql closeTheDataBase];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUserInterface];
}


- (void)initUserInterface{
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.alpha = 1;
    self.inputTextFiled.delegate = self;
}

#pragma mark - KeyBoard Notification
- (void)keyBoardWillShow:(NSNotification *)aNoti {
    NSDictionary * info = [aNoti userInfo];
    NSValue * value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    self.keyboardSize = [value CGRectValue].size;
    CGFloat Y = SCREEN_HEIGHT - self.bottomView.frame.origin.y - 44;
    if (self.keyboardSize.height > Y) {
        self.view.transform = CGAffineTransformMakeTranslation(WiiZero, - (self.keyboardSize.height - Y));
    }
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    [self.inputTextFiled resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];

}
#pragma mark - Button handleEvent
- (IBAction)handleEvent:(UIButton *)sender{
    if(!self.isSend){
        [self.inputTextFiled resignFirstResponder];
        [self dismissViewControllerAnimated:YES completion:nil];
    } else{
        if ([self.inputTextFiled.text length] != WiiZero) {
            [self.sql openDataBase];
            [self.sql insertDataSourceToDBwithArray:@[self.inputTextFiled.text,[WiiKit getCurrenTimeForSystem],@"从不",@"",@"",[WiiKit getCurrenDateForSystem],@""] tableName:TableName_MyTask];
            self.isSend = NO;
            [self dismissViewControllerAnimated:YES completion:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"notification"
                                                                    object:@"refresh"];
            }];
        } else{
            [self presentViewController:[WiiKit alertControllerWithMessage:@"待办事项不能为空！"] animated:YES completion:nil];
        }
    }
}


#pragma mark - TextFiled NSNotification 监听文本输入框
- (void)textFieldChange:(NSNotification*)notifice {
    // 注意如果有多个UITextFiled，需要加个判断
    UITextField *textField = [notifice object];
    if ([textField.text length] > 0 ) {
        self.isSend = YES;
        [self WiiButtonAnimation];
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            self.closeAndSendButton.transform = CGAffineTransformIdentity;
            [self.view layoutIfNeeded];
        }];
    }
}

#pragma mark - Animation

- (void)WiiButtonAnimation{
    [UIView animateWithDuration:0.5 animations:^{
        self.closeAndSendButton.transform = CGAffineTransformMakeRotation( - M_PI_2);
        [self.view layoutIfNeeded];
    }];

}

#pragma mark - setter/getter

- (XYSqlite3 *)sql{
    if (!_sql) {
        _sql = [[XYSqlite3 alloc]init];
    }
    return _sql;
}
@end
